package com.car.android.sdk;

import android.text.TextUtils;
import android.util.Log;

import com.car.android.sdk.ad.CarVideoIAP;
import com.car.android.sdk.bridge.CarCocosBridge;
import com.car.android.sdk.kwai.CarKwaiReward;
import com.car.android.sdk.manager.CarVideoManger;
import com.car.android.sdk.max.CarMaxReward;
import com.car.android.sdk.utils.CarSPUtils;

import org.json.JSONObject;

public class CarAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new CarAdSdk.VideoRewardCallback() {
            @Override
            public void onAdPlay() {
                adCallBack("ad_play_w",ad_source);
            }

            @Override
            public void onAdOver() {
                adCallBack("ad_over_w",ad_source);
            }

            @Override
            public void onAdError() {
                CarCocosBridge.getInstance().XSSdkCallback("ad_error_w", "{}");
            }
        });
    }

    private static void adCallBack(String state,String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source",ad_source);
            CarCocosBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void showAd(VideoRewardCallback callback) {
        CarVideoIAP service;
        double ecpm1 =  CarMaxReward.getInstance().getEcpm();
        double ecpm2 =  CarKwaiReward.getInstance().getEcpm();
        if (ecpm1>=ecpm2){
            service=CarVideoManger.getReward1Instance();
            Log.e("CCMatchW","show max reward");
        }else{
            service=CarVideoManger.getReward2Instance();
            Log.e("CCMatchW","show kwai reward");
        }

        if (service == null) {
            callback.onAdError();
            return;
        }
        showAd(callback, service);
    }

    public static void showAd(VideoRewardCallback callback, CarVideoIAP service) {
        try {
            service.showVideo(CarCocosBridge.mainGameActivity.get(), new OnAdShowListner() {
                @Override
                public void reward(boolean is ) {
                    try {
                        if (callback != null) {
                            callback.onAdOver();
                        }
                    } catch (Exception e) {
                    }
                }

                @Override
                public void error(String errorCode) {
                    try {
                        if (callback != null) {
                            callback.onAdError();
                        }
                    } catch (Exception e) {
                    }
                }

                @Override
                public void startPlay() {
                    try {
                        if (callback != null) {
                            callback.onAdPlay();
                        }
                    } catch (Exception e) {
                    }
                }
            });

            String country = CarSPUtils.getString(Constant.KEY_COUNTRY,"");
            if (!TextUtils.isEmpty(country)) {
                if (Constant.COUNTRY_ID.equals(country) || Constant.COUNTRY_BR.equals(country)) {
                    //检查kwai是否需要load
                    if (service instanceof CarMaxReward) {
                        CarKwaiReward.getInstance().loadRewardAd();
                    }
                }
            }
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }
    }

    public interface VideoRewardCallback {
        void onAdPlay();

        void onAdOver();

        void onAdError();
    }

    public interface OnAdShowListner {
        void reward(boolean is);

        void error(String errorCode);

        void startPlay();
    }

}
